<?php

namespace App\Http\Controllers\Api\V1;

use App\B2BProject\Custom\Member;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;

class AuthController extends Controller
{
    /**
     * 登录
     *
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function postLogin(Request $request)
    {
        if (\Auth::attempt($request->only(['user', 'password']))) {
            $user = \Auth::user();

            if ($user->class == Member::PROVIDER_USER) {
                return error_return('100002', 403);
            }

            $user->client_token = $token = Str::random(60);
            $user->save();

            return success_return(['token' => $token]);
        }

        return error_return('100001', 403);
    }

    /**
     * 注销
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function postLogout()
    {
        $user = \Auth::user();

        if (!is_null($user)) {
            $user->client_token = null;
            $user->save();

            \Auth::logout();
        }

        return success_return(null);
    }
}
